var offset = 6;
var p = false;
$.ajaxSetup({
    headers: { 'X-CSRF-TOKEN' : '{{ csrf_token() }}' }
});
 $(window).scroll(function () {
    if (($(window).height() + $(window).scrollTop()) >= $(document).height()) {
        //setTimeout(insertcode, 1000);/*IE 不支持*/
        // console.log(offset);
        $.ajax({
            type:'GET',
            url:"https://www.fxpixels.site/lv5/public/index.php/lazyindex",
            async: false,
            data: {
                'offset': offset,
                _token: $('meta[name="csrf-token"]').attr('content'),
            },
            contentType: 'application/json',
            processData: true,
            datatype: "json",
            success: function(data){
                // console.log(data);
                var article = '';
                var i;
                if (data.length != 0){
                    for(i=0; i < data.length; i++){
                        console.log(data[i].cover_img);
                        article_body = data[i].body.substr(0, 50)+'...';
                        article += '<hr style="margin: 5px 0; width:100%; height:5px;">'+
                        '<li style="margin: 40px 0; list-style:none;">' + 
                        '<div class="row"><div class="col-md-8 col-xs-7"><img style="width: 32px; height:32px; border-radius: 16px;" src="/lv5/public/img/user/'+ data[i].has_many_users[0].avatar +'" alt="">' +
                        ' <span>'+ data[i].has_many_users[0].name +'</span>&nbsp;&nbsp;&nbsp;&nbsp;<span>发表于&nbsp;&nbsp;'+ data[i].created_at +'</span><div class="title">'+
                        '<a style="color: #333333;" href="https://www.fxpixels.site/lv5/public/index.php/article/'+ data[i].id +'"><h4 style="font-weight:bold;">' + data[i].title + '</h4></a></div>' +
                        '<div class="body"><p>' + article_body + '</p></div><div>'+
                        '<a href="https://www.fxpixels.site/lv5/public/index.php/category?name='+ data[i].category +'"><span style="border: 1px solid #F1907F; padding: 2px 5px; border-radius: 3px; color:#F1907F; ">'+ data[i].category +'</span></a>'+
                        '&nbsp;&nbsp;&nbsp;阅读&nbsp;(<span>'+ data[i].read_total +'</span>)'+
                        '&nbsp;&nbsp;&nbsp;回复&nbsp;<span>'+ data[i].reply_total +'</span>'+
                        '&nbsp;&nbsp;&nbsp;点赞&nbsp;<span>'+ data[i].upvote_total +'</span></div></div>'+
                        '<div class="col-md-4 col-xs-5"><a href="https://www.fxpixels.site/lv5/public/index.php/article/'+ data[i].article_id +'">'+
                        '<img class="img-responsive" style="width:200px; height:150px; border-radius:12px;" src="https://www.fxpixels.site/lv5/public/img/cover/' + data[i].cover_img + '"' + "alt=''>"+
                        '</a></div></div></li>';
                    }
                    $('#index-articles').append(article);
                    p = false;
                }else if(p == false && data.length == 0){
                    $('#index-articles').append('<hr style="margin: 5px 0; width:100%; height:5px;">'+
                    '<li style="margin: 40px 0; list-style:none;">没有更多文章了~</li>');
                    p = true;
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                console.log('XMLHttpRequest.status='+XMLHttpRequest.status);
                 console.log('XMLHttpRequest.readyState='+XMLHttpRequest.readyState);
                 console.log('atextStatus='+textStatus);
            }
        });
        offset += 6;
    }
 });  